#include "umldiritem.h"

UmlDirItem::UmlDirItem(UmlProject *project) : UmlGraphicsItem(project)
{
    this->_itemTypeFlags |= UmlItem::UmlPackageItemType;
    this->_parentDir = 0;

    this->_pen.setWidth(1);
    this->_pen.setColor(Qt::black);

    this->_textItem.setFont(this->_boldFont);
    this->addToGroup(&this->_textItem);

    this->updateBoundingBox();
}

UmlDirItem::~UmlDirItem()
{
    qDebug() << "deleting umlDirItem: " << this->_name.toLocal8Bit();

    while (!this->_childItems.isEmpty()) delete this->_childItems.takeFirst();
}

UmlDatatype UmlDirItem::umlDataType()
{
    return PACKAGE;
}

void UmlDirItem::setText(QString name)
{
    UmlGraphicsItem::setText(name);

    this->_textItem.setPlainText(name);

    this->updateBoundingBox();
}

void UmlDirItem::appendChild(UmlGraphicsItem *item)
{
    item->setParentDir(this);
    item->setParent(this->scene());
    this->_childItems.append(item);
}

void UmlDirItem::removeChild(UmlGraphicsItem *item)
{
    this->_childItems.removeOne(item);
}

QList<UmlGraphicsItem*> UmlDirItem::items()
{
    return this->_childItems;
}

UmlGraphicsItem* UmlDirItem::child(int row)
{
    return this->_childItems.at(row);
}

int UmlDirItem::childCount() const
{
    return this->_childItems.count();
}

void UmlDirItem::updateBoundingBox()
{
    int textHeight = qRound(this->_textItem.boundingRect().height());
    int textWidth = qRound(this->_textItem.boundingRect().width());
    qreal penOffset = this->_pen.widthF() / (double)2;

    // setting shape
    QPolygonF dirShape;
    dirShape
            << QPointF(0, 0)
            << QPointF(0, 10 + 10 + textHeight + 10)
            << QPointF(10 + textWidth + 10, 10 + 10 + textHeight + 10)
            << QPointF(10 + textWidth + 10, 10)
            << QPointF(40, 10)
            << QPointF(40, 0)
            << QPointF(0, 0);

    this->_shape = QPainterPath();
    this->_shape.addPolygon(dirShape);

    this->_borderItem.setPos(QPointF(0, 0));
    this->_borderItem.setPolygon(dirShape);

    this->_textItem.setPos(10, 20);

    this->prepareGeometryChange();
    this->_boundingRect = this->_shape.boundingRect().adjusted(-penOffset, -penOffset, penOffset, penOffset);
}

QPainterPath UmlDirItem::shape() const
{
    return this->_shape;
}

void UmlDirItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    painter->fillPath(this->_shape, Qt::white);
}

QVariant UmlDirItem::itemChange(GraphicsItemChange change, const QVariant &value)
{
    if (change == QGraphicsItem::ItemSceneHasChanged) {
        this->_textItem.setParentItem(this);
        this->addToGroup(&this->_textItem);

        this->_borderItem.setParentItem(this);
        this->addToGroup(&this->_borderItem);

        this->updateBoundingBox();
    }

    return UmlGraphicsItem::itemChange(change, value);
}

void UmlDirItem::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    if (event->button() == Qt::LeftButton) {
        this->openEditor();
    }
}

QList<QAction*> UmlDirItem::contextActions()
{
    QList<QAction*> actions = UmlGraphicsItem::contextActions();

    QAction *separator1 = new QAction(this);
    separator1->setSeparator(true);
    actions.prepend(separator1);

    QAction *abstractAction = new QAction(tr("open package"), this);
    abstractAction->setIcon(QIcon(":icons/contextmenu/open-package.png"));
    actions.prepend(abstractAction);
    QObject::connect(abstractAction, SIGNAL(triggered()), this, SLOT(openPackage()));

    return actions;
}

void UmlDirItem::openEditor()
{
    PackageEditor *editor = new PackageEditor(this);

    if (editor->exec() == QDialog::Accepted) {
        this->updateBoundingBox();
        this->update();
    }
}

void UmlDirItem::openPackage()
{
    this->_project->drawPackage(this);
}
